package com.shg.web.action;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.shg.bean.Ad;
import com.shg.bean.Book;
import com.shg.bean.BookType;
import com.shg.bean.PageView;
import com.shg.bean.QueryResult;
import com.shg.bean.Shop;
import com.shg.service.AdService;
import com.shg.service.BookService;
import com.shg.service.BookTypeService;
import com.shg.service.ShopService;
import com.shg.service.SoldBookService;
import com.shg.web.tools.WebTool;


@Controller
public class ShopAction {
	//店铺的ID
	private String shopId;
	//浏览历史
	private List<Book> historyBook;
	//畅销书籍列表
	private List<Book> sellerbook;
	//店铺
	private Shop shop;
	//精品推荐
	private PageView<Book> pageView;
	//新书
	private PageView<Book> newview;
	//每页显示的最大记录数
	private int maxResult=3;
	//当前页
	private int page=1;
	//新书
	private int newResult=3, newPage=1;
	//
	private List<Ad> ads;
	
	private String message;
	//书籍类别导航
	private Set<BookType> bookTypes;
	
	@Resource BookService bookService;
	@Resource SoldBookService soldBookService;
	@Resource ShopService shopService;
	@Resource BookTypeService bookTypeService;
	@Resource AdService adService;
	/**
	 * 进入店铺
	 */
	public String intoshop(){
		try {
			shop = shopService.getIntoshop(shopId);
			ads = adService.getShop(shopId, 4);
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
		return "intoshop";
	}
	/**
	 * 获取到店铺精品推荐c
	 * @return
	 */
	public String recommend(){
		pageView = new PageView<Book>(maxResult, page);
		QueryResult<Book> result;
		try {
			result = bookService.getRecommend(shop.getId(), pageView.getFirstResult(), pageView.getMaxrecord());
			pageView.setQueryResult(result);
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
		return "recommend";
	}
	/**
	 * 店铺的新书热卖
	 * @return
	 */
	public String newBook(){
		newview=new PageView<Book>(newResult, newPage);
		QueryResult<Book> newResult;
		try {
			newResult = bookService.getNewBook(shop.getId(), newview.getFirstResult(), newview.getMaxrecord());
			newview.setQueryResult(newResult);
		} catch (Exception e) {
			e.printStackTrace();
			this.message="查询店铺最新书籍产生异常";
			return "error";
		}
		return "newBook";
	}
	/**
	 * 书籍类别
	 * @return
	 */
	public String booktype(){
		try {
			bookTypes = bookTypeService.getShopBookType(shopId);
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作出现异常";
			return "error";
		}
		return "booktype";
	}
	/**
	 * 浏览历史
	 * @return
	 */
	public String history(){
		if(WebTool.historyId()!=null)
			try {
				historyBook =  bookService.getHistory(Book.class, WebTool.historyId());
			} catch (Exception e) {
				e.printStackTrace();
				this.message="历史查询出现异常";
				return "error";
			}
		return "history";
	}
	/**
	 * 畅销书
	 * @return
	 */
	public String bestseller(){
		if(shopId!=null)
			try {
				sellerbook = soldBookService.getBestSeller(shopId, 10);
			} catch (Exception e) {
				e.printStackTrace();
				this.message="操作发生异常";
				return "error";
			}
		return "bestseller";
	}
	
	public String getShopId() {
		return shopId;
	}
	public void setShopId(String shopId) {
		this.shopId = shopId;
	}
	public List<Book> getHistoryBook() {
		return historyBook;
	}
	public void setHistoryBook(List<Book> historyBook) {
		this.historyBook = historyBook;
	}
	public List<Book> getSellerbook() {
		return sellerbook;
	}
	public void setSellerbook(List<Book> sellerbook) {
		this.sellerbook = sellerbook;
	}
	public Shop getShop() {
		return shop;
	}
	public void setShop(Shop shop) {
		this.shop = shop;
	}
	public PageView<Book> getPageView() {
		return pageView;
	}
	public void setPageView(PageView<Book> pageView) {
		this.pageView = pageView;
	}
	public int getMaxResult() {
		return maxResult;
	}
	public void setMaxResult(int maxResult) {
		this.maxResult = maxResult;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public PageView<Book> getNewview() {
		return newview;
	}
	public void setNewview(PageView<Book> newview) {
		this.newview = newview;
	}
	public int getNewResult() {
		return newResult;
	}
	public void setNewResult(int newResult) {
		this.newResult = newResult;
	}
	public int getNewPage() {
		return newPage;
	}
	public void setNewPage(int newPage) {
		this.newPage = newPage;
	}
	public Set<BookType> getBookTypes() {
		return bookTypes;
	}
	public void setBookTypes(Set<BookType> bookTypes) {
		this.bookTypes = bookTypes;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public List<Ad> getAds() {
		return ads;
	}
	public void setAds(List<Ad> ads) {
		this.ads = ads;
	}
	
}
